package com.cy.ppj.web;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.exception.MsgException;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.dto.manager.ManagerLoginDTO;
import com.cy.ppj.model.dto.user.UserLoginDTO;
import com.cy.ppj.model.enums.SmsType;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;

/**
 * @description:
 * @author: Ocean
 * @create: 2018-07-17 16:58
 **/
public class BaseController {

    @Autowired
    protected RedisTemplate redisTemplate;
    @Autowired
    protected HttpServletRequest request;

    /**
     * 校验错误信息
     * @param bindingResult
     */
    protected void checkError(BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            return;
        }
        String message = bindingResult.getAllErrors().get(0).getDefaultMessage();
        throw new MsgException(message);
    }

    /**
     * 检验短信验证码
     * @param mobile
     * @param code
     * @param type
     */
    protected void checkSmsCode(String mobile, String code, SmsType type) {
        String key = CommonUtils.smsCodeKey(mobile, type);
        Object obj = redisTemplate.opsForValue().get(key);
        if (obj == null) {
            throw new MsgException("验证码已失效。");
        }

        if (!String.valueOf(obj).equals(code)) {
            throw new MsgException("验证码已失效。");
        }
    }

    /**
     * 获取前台用户登录信息
     * @return
     */
    protected UserLoginDTO getUser() {
        String token = request.getHeader("token");
        if (token == null) {
            throw new MsgException("没有发现token");
        }
        String key = Constants.USER_LOGIN_KEY.concat(token);
        UserLoginDTO userLoginDTO = (UserLoginDTO) redisTemplate.opsForValue().get(key);
        return userLoginDTO;
    }

    /**
     * 根据token 获取用户信息
     * @param token
     * @return
     */
    protected UserLoginDTO getUser(String token) {
        String key = Constants.USER_LOGIN_KEY.concat(token);
        UserLoginDTO userLoginDTO = (UserLoginDTO) redisTemplate.opsForValue().get(key);
        return userLoginDTO;
    }

    /**
     * 获取后台管理员登录信息
     * @return
     */
    protected ManagerLoginDTO getManager() {
    	String token = request.getHeader("token");
    	if (token == null) {
            throw new MsgException("没有发现token");
        }
    	String key = Constants.ADMIN_LOGIN_KEY.concat(token);
    	ManagerLoginDTO managerLoginDTO = (ManagerLoginDTO) redisTemplate.opsForValue().get(key);
    	return managerLoginDTO;
    }
    
    protected Result ok() {
        return Result.success();
    }

    protected Result ok(String message, Object value) {
        return Result.success(message, value);
    }

    protected Result ok(String message) {
        return Result.success(message);
    }

    protected Result fail(String message) {
        return Result.fail(message);
    }

    protected void state(boolean expression, String message) {
        if (!expression) {
            throw new MsgException(message);
        }
    }

}
